<%if @organization_id == 1%>
<div class="updateStatus">
<h3>Result</h3>
<table border="0" cellspacing="0">
<tr class="updateHeader">
<th>Class</th>
<th>Section</th>
<th>Username</th>
<%@examHeaders.each do|header|%>
<th><%="#{header.examname}"%></th>
<%end%>
<th>Grand Total</th>
<th>Percentage</th>

</tr>
<% @arr = []%>
<%@userIds.each do|user|%>
<tr class="updateAltrRow">
	
<% @arr << "#{user.userId}" %>

<td> <%="#{user.coursename}"%></td>	
<td> <%="#{user.section}"%></td>
<td> <%="#{user.username}"%></td>

<% samp = "SELECT  Distinct U.id as userId, U.name as username, F.id as categoryId, M.name as coursename, S.name
    From exam_results E
    Inner Join categoryexams C on E.categoryexam_id = C.id
    Inner Join categoryusers D on E.categoryuser_id = D.id
    Inner Join categories F on C.category_id = F.id
    Inner Join exams S on C.exam_id = S.id
    Inner Join users U on D.user_id = U.id
    Inner Join courses M on F.course_id = M.id
    Inner Join sections N on F.section_id = N.id
    where F.id = #{user.categoryId} and C.category_id = F.id and D.user_id = #{user.userId};"%>
  <% @get = ExamResult.find_by_sql(samp)%>
 
 <%@grandTotal = []%>
 <%@totalMark = []%>
<% @get.each do|subject|%>

 <td>
 	<% marks = "SELECT M.name as coursename,S.name,S.subject,S.id,E.attempt,E.total_mark as totalMark,E.score,E.percent,E.status
    From exam_results E
    Inner Join categoryexams C on E.categoryexam_id = C.id
    Inner Join categoryusers D on E.categoryuser_id = D.id
    Inner Join categories F on C.category_id = F.id
    Inner Join exams S on C.exam_id = S.id
    Inner Join users U on D.user_id = U.id
    Inner Join courses M on F.course_id = M.id
    Inner Join sections N on F.section_id = N.id
    where F.id = #{subject.categoryId} and U.id = #{subject.userId} and S.name = '#{subject.name}'
    Group By F.id,S.id,U.name,S.name,E.attempt,S.total_mark,E.percent;" %>
	
	<% getMarks = ExamResult.find_by_sql(marks)%>
 	<%getMarks.each do|g|%>
	<%@grandTotal << "#{g.score}".to_f%>
	<%@totalMark << "#{g.totalMark}".to_f%>
	<%="#{g.score}"%> (<%="#{g.status}"%>)
	<%end%>

 </td>
 


<%end%>

 <td>

	<%=@grandTotal.inject{|sum,x| sum + x }%>
	<%gt = @grandTotal.inject{|sum,x| sum + x }%>
    <%tot = @totalMark.inject{|sum,x| sum + x }%>
	
 </td>
 <td><%=gt/tot * 100.to_f.round(2)%></td>


</tr>
<%end%>

</table>
</div>

<%end%>

<%if @organization_id == 2%>
<div class="updateStatus">
<h3>Result</h3>
<table border="0" cellspacing="0">
<tr class="updateHeader">
<th>Course</th>
<th>Year</th>
<th>Username</th>
<%@examHeaders.each do|header|%>
<th><%="#{header.examname}"%></th>
<%end%>
<th>Grand Total</th>
<th>Percentage</th>

</tr>
<% @arr = []%>
<%@userIds.each do|user|%>
<tr class="updateAltrRow">
	
<% @arr << "#{user.userId}" %>

<td> <%="#{user.coursename}"%></td>	
<td> <%="#{user.AcademicYear}"%></td>
<td> <%="#{user.username}"%></td>

<% samp = "SELECT  Distinct U.id as userId, U.name as username, F.id as categoryId, M.name as coursename,A.name as AcademicYear, S.name
    From exam_results E
    Inner Join categoryexams C on E.categoryexam_id = C.id
    Inner Join categoryusers D on E.categoryuser_id = D.id
    Inner Join categories F on C.category_id = F.id
    Inner Join exams S on C.exam_id = S.id
    Inner Join users U on D.user_id = U.id
    Inner Join academic_years A on F.academic_year_id = A.id
    Inner Join courses M on F.course_id = M.id
    Inner Join departments N on F.department_id = N.id
   Inner Join semesters P on F.semester_id = P.id
    where F.id = #{user.categoryId} and C.category_id = F.id and D.user_id = #{user.userId};"%>
  <% @get = ExamResult.find_by_sql(samp)%>
 
 <%@grandTotal = []%>
 <%@totalMark = []%>
<% @get.each do|subject|%>

 <td>
 	<% marks = "SELECT M.name as coursename,A.id as AcademicYear,S.name,S.subject,S.id,E.attempt,E.total_mark as totalMark,E.score,E.percent,E.status
    From exam_results E
    Inner Join categoryexams C on E.categoryexam_id = C.id
    Inner Join categoryusers D on E.categoryuser_id = D.id
    Inner Join categories F on C.category_id = F.id
    Inner Join exams S on C.exam_id = S.id
    Inner Join users U on D.user_id = U.id
    Inner Join academic_years A on F.academic_year_id = A.id
    Inner Join courses M on F.course_id = M.id
    Inner Join departments N on F.department_id = N.id
   Inner Join semesters P on F.semester_id = P.id
    where F.id = #{subject.categoryId} and U.id = #{subject.userId} and S.name = '#{subject.name}'
    Group By F.id,S.id,U.name,S.name,E.attempt,S.total_mark,E.percent;" %>
	
	<% getMarks = ExamResult.find_by_sql(marks)%>
 	<%getMarks.each do|g|%>
	<%@grandTotal << "#{g.score}".to_f%>
	<%@totalMark << "#{g.totalMark}".to_f%>
	<%="#{g.score}"%> (<%="#{g.status}"%>)
	<%end%>

 </td>
 


<%end%>

 <td>

	<%=@grandTotal.inject{|sum,x| sum + x }%>
	<%gt = @grandTotal.inject{|sum,x| sum + x }%>
    <%tot = @totalMark.inject{|sum,x| sum + x }%>
	
 </td>
 <td><%=gt/tot * 100.to_f.round(2)%></td>


</tr>
<%end%>

</table>
</div>

<%end%>


<%if @organization_id == 3 or @organization_id == 4%>
<div class="updateStatus">
<h3>Result</h3>
<table border="0" cellspacing="0">
<tr class="updateHeader">
<th>Department</th>
<th>Username</th>
<%@examHeaders.each do|header|%>
<th><%="#{header.examname}"%></th>
<%end%>
<th>Grand Total</th>
<th>Percentage</th>

</tr>
<% @arr = []%>
<%@userIds.each do|user|%>
<tr class="updateAltrRow">
	
<% @arr << "#{user.userId}" %>

<td> <%="#{user.department}"%></td>	
<td> <%="#{user.username}"%></td>

<% samp = "SELECT  Distinct U.id as userId, U.name as username, F.id as categoryId, N.name as department, S.name
    From exam_results E
    Inner Join categoryexams C on E.categoryexam_id = C.id
    Inner Join categoryusers D on E.categoryuser_id = D.id
    Inner Join categories F on C.category_id = F.id
    Inner Join exams S on C.exam_id = S.id
    Inner Join users U on D.user_id = U.id
    Inner Join departments N on F.department_id = N.id
    where F.id = #{user.categoryId} and C.category_id = F.id and D.user_id = #{user.userId};"%>
  <% @get = ExamResult.find_by_sql(samp)%>
 
 <%@grandTotal = []%>
 <%@totalMark = []%>
<% @get.each do|subject|%>

 <td>
 	<% marks = "SELECT N.name as department,S.name,S.subject,S.id,E.attempt,E.total_mark as totalMark,E.score,E.percent,E.status
    From exam_results E
    Inner Join categoryexams C on E.categoryexam_id = C.id
    Inner Join categoryusers D on E.categoryuser_id = D.id
    Inner Join categories F on C.category_id = F.id
    Inner Join exams S on C.exam_id = S.id
    Inner Join users U on D.user_id = U.id
    Inner Join departments N on F.department_id = N.id
    where F.id = #{subject.categoryId} and U.id = #{subject.userId} and S.name = '#{subject.name}'
    Group By F.id,S.id,U.name,S.name,E.attempt,S.total_mark,E.percent;" %>
	
	<% getMarks = ExamResult.find_by_sql(marks)%>
 	<%getMarks.each do|g|%>
	<%@grandTotal << "#{g.score}".to_f%>
	<%@totalMark << "#{g.totalMark}".to_f%>
	<%="#{g.score}"%> (<%="#{g.status}"%>)
	<%end%>

 </td>
 


<%end%>

 <td>

	<%=@grandTotal.inject{|sum,x| sum + x }%>
	<%gt = @grandTotal.inject{|sum,x| sum + x }%>
    <%tot = @totalMark.inject{|sum,x| sum + x }%>
	
 </td>
 <td><%=gt/tot * 100.to_f.round(2)%></td>


</tr>
<%end%>

</table>
</div>

<%end%>
